﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using TruckScale.Helpers;
using static TruckScale.Helpers.LogHelper;

namespace TruckScale.Models
{
  public class GoodsModel
  {
    public int? GoodsId { get; set; }
    public string GoodsNum { get; set; }
    public string GoodsName { get; set; }
    public string GoodsPrice { get; set; }
    public string Comment { get; set; }
    public bool IsValid { get; set; }
    public string By1 { get; set; }
    public string By2 { get; set; }
    public string By3 { get; set; }

    public static List<GoodsModel> SelectAllGoods()
    {
      using (IDbConnection cnn = DBHelper.DbConnection())
      {
        try
        {
          string sql = "select * from goods;";
          IEnumerable<GoodsModel> output = cnn.Query<GoodsModel>(sql);
          return output.ToList();
        }
        catch (Exception e)
        {
          SetLog(e.Message, LogType.OperationLogger, LogLevel.Error);
          return null;
        }
      }
    }
    public static List<GoodsModel> SelectValidGoods()
    {
      using (IDbConnection cnn = DBHelper.DbConnection())
      {
        try
        {
          string sql = @"select * from goods where is_valid = 1;";
          IEnumerable<GoodsModel> output = cnn.Query<GoodsModel>(sql);
          return output.ToList();
        }
        catch (Exception e)
        {
          SetLog(e.Message, LogType.OperationLogger, LogLevel.Error);
          return null;
        }
      }
    }
    public static List<GoodsModel> SelectValidGoods(string name)
    {
      using (IDbConnection cnn = DBHelper.DbConnection())
      {
        try
        {
          string sql = $"select * from goods where is_valid = 1 and (goods_name like '%{name}%' or goods_num like '%{name}%');";
          IEnumerable<GoodsModel> output = cnn.Query<GoodsModel>(sql);
          return output.ToList();
        }
        catch (Exception e)
        {
          SetLog(e.Message, LogType.OperationLogger, LogLevel.Error);
          return null;
        }
      }
    }

    public static int SelectUsedGoods(GoodsModel goods)
    {
      using (IDbConnection cnn = DBHelper.DbConnection())
      {
        try
        {
          string sql = $"select count(*) from weighrecord where is_valid = 1 and wz = '{goods.GoodsId}';";
          int output = cnn.Query<int>(sql).FirstOrDefault();
          return output;
        }
        catch (Exception e)
        {
          SetLog(e.Message, LogType.OperationLogger, LogLevel.Error);
          return -1;
        }
      }
    }
    public static int InsertGoods(GoodsModel goods)
    {
      using (IDbConnection cnn = DBHelper.DbConnection())
      {
        try
        {
          string sql = "insert into goods (goods_num, goods_name, goods_price, comment) values (@GoodsNum, @GoodsName, @GoodsPrice, @Comment);";
          return cnn.Execute(sql, goods);
        }
        catch (Exception e)
        {
          SetLog(e.Message, LogType.OperationLogger, LogLevel.Error);
          return -1;
        }
      }
    }
    public static int UpdateGoods(GoodsModel goods)
    {
      using (IDbConnection cnn = DBHelper.DbConnection())
      {
        try
        {
          string sql = "update goods set goods_num = @GoodsNum, goods_name = @GoodsName, goods_price = @GoodsPrice, comment = @Comment , is_valid = @IsValid where goods_id = @GoodsId;";
          return cnn.Execute(sql, goods);
        }
        catch (Exception e)
        {
          SetLog(e.Message, LogType.OperationLogger, LogLevel.Error);
          return -1;
        }
      }
    }
    public static int DeleteGoods(GoodsModel goods)
    {
      using (IDbConnection cnn = DBHelper.DbConnection())
      {
        try
        {
          string sql = $"delete from goods where goods_id = '{goods.GoodsId}'";
          return cnn.Execute(sql);
        }
        catch (Exception e)
        {
          SetLog(e.Message, LogType.OperationLogger, LogLevel.Error);
          return -1;
        }
      }
    }
  }
}
